var benchmarkLib = [];
var testCSV = jsCSVTester.genCSV(2000,["firstName","lastName","favoriteCar","favoriteColors","favoriteMonths"]);

(function (){
    var test = { 
        name : "dataset test",
        code : function (){
               

                var nCSVData = (nCSV.parse(testCSV)).data;
                var jsCSVData = (jsCSV.parse(testCSV)).data;

                deepEqual (nCSVData, jsCSVData, "csv parsing: the same data");
            }
    };
    benchmarkLib.push(test); //comment this line to remove from the test
})();   


(function (){

    function benchmark(name, ref, test, iterations){
        var min = undefined; var max = undefined; 
        var count = iterations || 1000;
        var total = 0;
        for(var i = 0; i < count; ++i){
            var start = (new Date()).valueOf();
            test();
            var duration = (new Date()).valueOf() - start;
            min = ( !min || min > duration ) ? duration : min ;
            max = ( !max || max < duration ) ? duration : max ;
            total += duration;
        }
        return {name:name,min:min,max:max,average:(total/count),ref:ref};
    }

    function tagAttributes(attributes){
        return ((attributes || {})["class"])?" class='"+attributes["class"]+"' ":"";
    }

    function divTag(content,attributes){
        return "<div"+tagAttributes(attributes)+">"+content+"</div>";
    }

    function benchmarkTemplate(benchmark){
        return divTag(
            divTag(""+benchmark.name, {"class":"name"})+ 
            divTag("min:"+benchmark.min, {"class":"min value"})+ 
            divTag("max:"+benchmark.max, {"class":"max value"})+ 
            divTag("average:"+benchmark.average, {"class":"average value"}),
            {"class":"benchmark"}
        );
    }
    
    function display(benchmark){
        $("#message").append(benchmarkTemplate(benchmark));
        $("input[type='text'][name='"+benchmark.ref+"']").val(benchmark.average);
    }

    runBenchmarks = function(){
        $("#message").text("sleeping 3 sec... and then running tests, it will take a few moments");
        setTimeout(function(){
                $("#message").text("");
                var iterations = 20;
                display(benchmark("nCSV","entry.0.single",function(){ nCSV.parse(testCSV); },iterations));
                display(benchmark("jsCSV","entry.1.single",function(){ jsCSV.parse(testCSV); },iterations));
                display(benchmark("jQuery-CSV","entry.2.single",function(){ $.csv.toArrays(testCSV); },iterations));
                $("input[type='text'][name='entry.3.single']").val(navigator.userAgent);
            },
        3000);
    }


})();
